'''
用户账户相关功能:注册  登录 注销
'''
import datetime
import uuid #随机字符串

from django.shortcuts import *
from django.http import *
from web.forms.account import *

def register(request):
    if request.method == 'GET':
        forms = RegModelFrom()
        return render(request, 'register.html', {'form': forms})
    form = RegModelFrom(data=request.POST)
    if form.is_valid():
        # 验证通过,写入数据库(密码密文)
        instance = UserRole.objects.create(**form.cleaned_data)
        instance = form.save()  # save()自动去除数据库没有的字段 如:重复密码,验证码
        return JsonResponse({'status': True, 'data': '/login/'})
    else:
        print(form.errors)
    return JsonResponse({'status': False, 'error': form.errors})

def login(request):
    '''用户名密码登录'''
    if request.method == 'GET':
        form = LoginForm(request)
        return render(request, 'login.html', {'form': form})
    form = LoginForm(request, data=request.POST)
    if form.is_valid():
        user_name = form.cleaned_data['user_name']

        pwd = form.cleaned_data['password']

        # user_object = UserInfo.objects.filter(name=name,pwd=pwd).first()
        from django.db.models import Q

        user_object = UserRole.objects.filter(user_name=user_name,password=pwd).first()

        if user_object:
            request.session['user_id'] = user_object.id
            request.session['user_name'] = user_object.real_name
            # request.session.set_expiry(60 * 60 * 24 * 14)
            return redirect(reverse('index'))
        form.add_error('name', '用户名或者密码错误')
    return render(request, 'login.html', {'form': form})



# def image_code(request):
#     '''生成图片验证码'''
#     from io import BytesIO
#     from utils.image_code import check_code
#
#     image_object, code = check_code()
#     request.session['image_code'] = code
#     request.session.set_expiry(60)  # 设置验证码过期时间,为60秒
#     stream = BytesIO()
#     image_object.save(stream, 'png')
#     stream.getvalue()
#     return HttpResponse(stream.getvalue())

def loginout(request):
    request.session.flush() #清除session内容

    return redirect(reverse('index'))

def index(request):
    all = CuriseInfo.objects.all()
    return render(request,'index.html',{'all':all})


def room(request,id):
    room_all = RoomInfo.objects.filter(curise=id)
    return render(request, 'room.html', {'room_all': room_all})

def market(request):
    if request.method == 'GET':
        Market_all = Market.objects.all()
        return render(request, 'Market.html', {'Market_all': Market_all})



def add(request,id):
    a = Market.objects.filter(pk=id)
    user_id = request.session.get('user_id', 0)
    for i in a:
        a = Consumption(amount=i.price,place=i.Mname,consume_type=i.classify,user_id=user_id)
    a.save()
    return HttpResponse('订单保存成功')

def reserves(request,id,cid):

    RoomInfo.objects.filter(pk=id,).update(check_in_state=2)
    room_all = RoomInfo.objects.filter(pk=id)
    user_id = request.session.get('user_id', 0)
    for i in room_all:
        a = i.price * 1.1
        dep =round(a,1)
        r = RoomUser(user_id=user_id,room_id=i.id,deposit_amount=dep,deposit_state=2,pre_room_id=0,is_now=1,curise_id=cid)
        r.save()
        return redirect(reverse('room',args=[i.curise_id]))
